package com.hl.card.pay.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.plugins.Page;
import com.hl.card.common.persistence.model.Recharge;
import com.hl.card.common.util.ResponseModel;
import com.hl.card.modular.pack.service.RechargeService;
import com.hl.card.pay.service.PayRechargeService;
import com.hl.card.pay.vo.RechargeVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * 充值
 */
@Controller
public class PayRechargeController {

    @Autowired
    private RechargeService rechargeService;

    @Autowired
    private PayRechargeService payRechargeService;

    /***
     * 充值第一步（生成订单）
     * @param vo
     * @return
     */
    @RequestMapping(value = "readyRecharge",method = RequestMethod.POST)
    @ResponseBody
    public ResponseModel mkOrder(RechargeVo vo){
            Recharge recharge=new Recharge();
            recharge.setUser_id(vo.getUserId());
            recharge.setRecharge_no("RECHARGE"+System.currentTimeMillis()+"");//生成订单号
            recharge.setCurrency(vo.getCurrency());
            recharge.setAmount(Double.parseDouble(vo.getAmount()));//币的数量
            recharge.setAdd_time(new Date());
            recharge.setExtra("RECHARGE");
            recharge.setStatus(0);
        ResponseModel responseModel = new ResponseModel();
        if(payRechargeService.checkRecharge(recharge.getUser_id(), recharge.getAmount())) {
            payRechargeService.saveToRedis(recharge);
            responseModel.setStatus("0000");
            responseModel.setData(recharge);
        }else {
            responseModel.setStatus("0001");
            responseModel.setData("充值超额");
        }
        return    responseModel;
    }
    /**
     * 第二步（填写转账地址）
     *
     * @param reChargeVo
     * @return
     */
    @RequestMapping(value = "doRechargeApi",method = RequestMethod.POST)
    @ResponseBody
    public JSONObject doRechargeApi(RechargeVo reChargeVo){
        String str= payRechargeService.doRecharge(reChargeVo);
        return    JSONObject.parseObject(str);
    }
    /**
     * 查询临时订单
     * @param userId
     * @return
     */
    @RequestMapping(value = "findRecharge",method = RequestMethod.GET)
    @ResponseBody
    public ResponseModel find(String userId){
        return    payRechargeService.findRecharge(userId);
    }
    /**
     *  根据币种查询临时订单
     * @param userId
     * @return
     */
    @RequestMapping(value = "findRechargeByCurrency",method = RequestMethod.GET)
    @ResponseBody
    public ResponseModel findByCurrency(String userId,String currency){
        return    payRechargeService.findRecharge(userId,currency);
    }
    /**
     *
     * @param
     * @return
     */
    @RequestMapping(value = "findHLRemark",method = RequestMethod.GET)
    @ResponseBody
    public ResponseModel findRemark(){
        ResponseModel responseModel=new ResponseModel();
        responseModel.setStatus("0000");
        responseModel.setData(payRechargeService.findRemark());
        return   responseModel;
    }

    @RequestMapping(value = "recharge/list",method = RequestMethod.GET)
    @ResponseBody
    public ResponseModel findRechargeLidt(String userId){
        ResponseModel responseModel=new ResponseModel();
        responseModel.setStatus("0000");
        responseModel.setData(payRechargeService.findRechargeList(userId));
        return   responseModel;
    }
    @RequestMapping("rechargeList")
    @ResponseBody
    public ResponseModel CollectList(String userId,Integer size, Integer current){
        ResponseModel responseModel=new ResponseModel();
        Recharge recharge=new Recharge();
        recharge.setUser_id(userId);
        recharge.setExtra("RECHARGE");
        Page<Recharge> dPage = rechargeService.selectMultiConditionLimit(recharge,size,current);
        Map<String, Object> jsonMap = new HashMap<String, Object>();
        jsonMap.put("total", dPage.getTotal());
        jsonMap.put("rows", dPage.getRecords());
        jsonMap.put("current", dPage.getCurrent());
        jsonMap.put("pages", dPage.getPages());
        responseModel.setStatus("0000");
        responseModel.setData(dPage);
        return  responseModel;
    }
    /**
     * 执行最终的支付(通过审核)
     * @param mchTransNo
     * @return
     */
    @PostMapping("doRechargeTrans")
    @ResponseBody
    public Object doTrans(String mchTransNo,String modifyAmount){
        return payRechargeService.doTrans(mchTransNo,modifyAmount);
    };
    //取消待转账充值
    @RequestMapping("/recharge/cancle")
    @ResponseBody
    public ResponseModel cancelRechage(String userId, String currency) {
        ResponseModel responseModel = new ResponseModel();
        payRechargeService.cancel(userId, currency);
        responseModel.setData("取消成功");
        responseModel.setStatus("0000");
        return responseModel;
    }
    //获取redis 内容
    @RequestMapping("/recharge/getRedis")
    @ResponseBody
    public String getRedisRechargeByKey(String key) {
        return   payRechargeService.getRedis(key);
    }
    /**
     * 微服务供 钱包调用充值
     *
     * @param userId 用户
     * @param currency 币种
     * @param amount 充值数量
     * @return
     */
    @RequestMapping("/recharge/tokenWallet")
    @ResponseBody
    public String tokenWallet( String userId, String currency,String amount,String remarkInfo,String hash){
        return payRechargeService.rechargeTokenWallet(userId,currency,amount,remarkInfo,hash);
    }

}
